/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created by dnoakes on 26-Nov-2002 18:29:12 using IntelliJ IDEA.
 */

package au.com.lastweekend.jim.io.metadata;

import java.io.Serializable;

/**
 * 
 * 
 * 
 * @version $Id: Tag.java,v 1.2 2005/06/09 11:15:35 ggardner Exp $
 * @author <a href="mailto:grant@lastweekend.com.au"> <Grant Gardner> </a>
 */
public class Tag implements Serializable {

    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 2264942428379448411L;
    private final int _tagType;
    private final int _formatCode;
    private final Directory _directory;

    public Tag(int tagType, int formatCode, Directory directory) {

        _tagType = tagType;
        _directory = directory;
        _formatCode = formatCode;
    }

    /**
     * Gets the tag type as an int
     * 
     * @return the tag type as an int
     */
    public int getTagType() {

        return _tagType;
    }

    /**
     * Gets the tag type in hex notation as a String with padded leading zeroes if necessary (i.e. <code>0x100E</code>).
     * 
     * @return the tag type as a string in hexadecimal notation
     */
    public String getTagTypeHex() {

        String hex = Integer.toHexString(_tagType);
        while (hex.length() < 4) {
            hex = "0" + hex;
        }
        return "0x" + hex;
    }

    /**
     * Get a description of the tag's value, considering enumerated values and units.
     * 
     * @return a description of the tag's value
     * @throws MetadataException
     */
    public String getDescription() throws MetadataException {

        return _directory.getDescription(_tagType);
    }

    /**
     * Get the name of the tag, such as <code>Aperture</code>, or <code>InteropVersion</code>.
     * 
     * @return the tag's name
     */
    public String getTagName() {

        return _directory.getTagName(_tagType);
    }

    /**
     * Get the name of the directory in which the tag exists, such as <code>Exif</code>,<code>GPS</code> or
     * <code>Interoperability</code>.
     * 
     * @return name of the directory in which this tag exists
     */
    public String getDirectoryName() {

        return _directory.getName();
    }

    /**
     * A basic representation of the tag's type and value in format: <code>FNumber - F2.8</code>.
     * 
     * @return the tag's type and value
     */
    @Override
    public String toString() {

        String description;
        try {
            description = getDescription();
        } catch (MetadataException e) {
            description = _directory.getString(getTagType()) + " (unable to formulate description)";
        }
        return "[" + _directory.getName() + "] " + getTagName() + " - " + description;
    }

    public int getFormatCode() {

        return _formatCode;
    }

    @Override
    public boolean equals(Object obj) {

        if (obj instanceof Tag) {
            Tag other = (Tag) obj;
            return (_tagType == other._tagType && _formatCode == other._formatCode);
        }
        return false;
    }

    @Override
    public int hashCode() {

        // Hmmm surely there is a proper way to do this....
        return _tagType & _formatCode;
    }

    public boolean isUpdateable() {

        return _directory.isTagUpdateable(_tagType);
    }

    /**
     * @param value
     */
    public void update(Object value) {

        _directory.setTag(_tagType, _formatCode, value);

    }
}